//////////////////////////////////////////////////////////////////////
//
// File: TextScreen.java
//
// Copyright (c) 2005 TiVo Inc.
//
//////////////////////////////////////////////////////////////////////

package com.tivo.hme.samples.bananas;

import com.tivo.hme.bananas.BApplicationPlus;
import com.tivo.hme.bananas.BButtonPlus;
import com.tivo.hme.bananas.BEvent;
import com.tivo.hme.bananas.BTextPlus;
import com.tivo.hme.bananas.BViewPlus;
import com.tivo.hme.bananas.layout.Layout;
import com.tivo.hme.bananas.layout.LayoutManager;
import java.awt.Color;

/**
 * An Example Screen to to show various attributes you can use when using the
 * BText widget. With the BText widget you can set colors, styles, alignment and
 * display an optional shadow.
 *
 * @author Carl Haynes
 */
public class TextScreen extends BananasSampleScreen
{
    /**
     * Constructor
     */
    public TextScreen(BApplicationPlus app)
    {
        super(app);

        LayoutManager lm = new LayoutManager(getNormal());
        Layout safeTitle = lm.safeTitle(this);
        Layout layout = safeTitle;

        layout = lm.indentX(layout, 32);
        layout = lm.height(layout, 30);
        layout = lm.valign(layout, A_TOP+5);
        Layout baseLayout = layout;
        
        //
        // A line of text left aligned.
        //

        layout = lm.relativeY(baseLayout, true, true);
        layout = lm.valign(layout, A_TOP+45);
        layout = lm.stretchWidth(layout);
        Layout alignRow = layout;
        
        layout = lm.halign(alignRow, A_LEFT);
        
        BTextPlus plainSample = new BTextPlus(getNormal(), layout);
        plainSample.setFlags(RSRC_HALIGN_LEFT | RSRC_VALIGN_TOP);
        plainSample.setValue("Left aligned text");
        plainSample.setFont("default-18.font");
 
        //
        // A line of text center aligned.
        //

        layout = lm.halign(alignRow, A_CENTER);
        
        BTextPlus centerAligned = new BTextPlus(getNormal(), layout);
        centerAligned.setFlags(RSRC_HALIGN_CENTER | RSRC_VALIGN_TOP);
        centerAligned.setValue("Centered text");
        centerAligned.setFont("default-18.font");
        
        //
        // A line of text right aligned.
        //

        layout = lm.halign(alignRow, A_RIGHT);
        
        BTextPlus rightAligned = new BTextPlus(getNormal(), layout);
        rightAligned.setFlags(RSRC_HALIGN_RIGHT | RSRC_VALIGN_TOP);
        rightAligned.setValue("Right aligned text");
        rightAligned.setFont("default-18.font");

        layout = lm.relativeY(baseLayout, true, true);
        
        //
        // A line of text with a bold style.
        //
        
        BTextPlus boldText = new BTextPlus(getNormal(), layout);
        boldText.setFlags(RSRC_HALIGN_LEFT);
        boldText.setFont("default-18-bold.font");
        boldText.setValue("Bold text");
        
        //
        // A line of text with italic style.
        //

        
        BTextPlus italicText = new BTextPlus(getNormal(), layout);
        italicText.setFlags(RSRC_HALIGN_CENTER);
        italicText.setFont("default-18-italic.font");
        italicText.setValue("Italic text");
        
        //
        // A line of text with bold/italic style.
        //
        
        BTextPlus boldItalicText = new BTextPlus(getNormal(), layout);
        boldItalicText.setFlags(RSRC_HALIGN_RIGHT);
        boldItalicText.setFont("default-18-bolditalic.font");
        boldItalicText.setValue("Bold italic text");


        layout = lm.relativeY(baseLayout, true, true);
        layout = lm.stretchWidth(layout, 1/3f);
        Layout styleRow = layout;
        
        //
        // A line of text width a custom color.
        //

        layout = lm.halign(styleRow, A_LEFT);
        
        BTextPlus coloredText = new BTextPlus(getNormal(), layout);
        coloredText.setFlags(RSRC_HALIGN_LEFT);
        coloredText.setFont("default-18-bolditalic.font");
        coloredText.setColor(new Color(150, 100, 100));
        coloredText.setValue("Colored text");


        //
        // A line of text with a custom shadow (custom color, 
        // offset by 5 pixels).
        //

        layout = lm.halign(styleRow, A_CENTER);
        
        BTextPlus shadowSample2 = new BTextPlus(getNormal(), layout);
        shadowSample2.setFlags(RSRC_HALIGN_LEFT | RSRC_VALIGN_TOP);
        shadowSample2.setShadow(new Color(255, 200, 200), 5);
        shadowSample2.setValue("Text with a custom shadow");
        shadowSample2.setFont("default-18.font");
        
        //
        // a line of text with a default shadow (black, one pixel offset).
        //

        layout = lm.halign(styleRow, A_RIGHT);
        
        BTextPlus shadowSample = new BTextPlus(getNormal(), layout);
        shadowSample.setFlags(RSRC_HALIGN_RIGHT);
        shadowSample.setShadow(true);
        shadowSample.setValue("Text with a default shadow");
        shadowSample.setFont("default-18.font");

        layout = lm.relativeY(baseLayout, true, true);
        layout = lm.size(layout, 300, 60);
        
        
        //
        // A wrapped line of text over a darkened area.
        //

        BViewPlus bg = new BViewPlus(getNormal(), layout);
        bg.setResource(new Color(25, 25, 50));
        bg.setTransparency(.5f);
        
        BTextPlus wrapText = new BTextPlus(getNormal(), layout);
        wrapText.setFlags(RSRC_HALIGN_LEFT | RSRC_TEXT_WRAP);
        wrapText.setFont("default-18.font");
        wrapText.setValue("This is a very long piece of text that will wrap because the RSRC_TEXT_WRAP flag is set.");

        //
        // Return to main menu button, set a left arrow highlight to call action
        // "pop" when selected.
        //
        
        layout = lm.valign(safeTitle, A_BOTTOM);
        layout = lm.width(layout, 300);
        
        BButtonPlus button = new BButtonPlus(getNormal(), layout);
        button.setValue("Return to main menu");
        button.setBarAndArrows(BAR_HANG, BAR_DEFAULT, "pop", null, null, null, true);
        setFocusDefault(button);
    }
    
    /**
     * Since there is only one button, we will treat select like a left 
     * action and pop the screen.
     */
    public boolean handleKeyPress(int code, long rawcode) 
    {
        switch (code) {
          case KEY_SELECT:
            postEvent(new BEvent.Action(this, "pop"));            
            return true;
        }
        return super.handleKeyPress(code, rawcode);
    }
    
    /**
     *  Title of the screen.
     */
    public String toString() 
    {
        return "BText";
    }
}
